package org.example.studentsystem_back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.DormStu;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.DormStuService;
import org.example.studentsystem_back.vo.DormStuMangeVo.DormStuVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

@Valid
@RestController
public class DormStuController {
    @Resource
    private DormStuService dormStuService;

    @PostMapping("/dormstu/live")
    public CommonResponse<DormStuVo> getDormStuLive(@RequestBody String  json) throws ParseException {
        JSONObject jsonObject = JSON.parseObject(json);
        PageDTO pageDTO=jsonObject.toJavaObject(PageDTO.class);
        String a=jsonObject.getString("num");
        int num;
        if(a==null|| a.isEmpty()){
            num=-1;
        }
        else {
            num=Integer.parseInt(a);
        }
        String name=jsonObject.getString("name");
        String id=jsonObject.getString("id");
        return dormStuService.findAll(pageDTO,id,num,name);
    }

    @PostMapping("/dormstu/live/add")
    public CommonResponse<DormStu> addDormStuLive(@Valid @RequestBody DormStu dormStu) throws ParseException {
        return dormStuService.add(dormStu);
    }

    @PostMapping("/dormstu/live/update")
    public CommonResponse<DormStu> updateDormStuLive(@Valid @RequestBody DormStu dormStu) {
        return dormStuService.update(dormStu);
    }

    @PostMapping("/dormstu/live/delete")
    public CommonResponse<DormStu> deleteDormStuLive(@Valid @RequestBody DormStu dormStu) {
        return dormStuService.delete(dormStu);
    }

    @PostMapping("/dormstu/absence")
    public CommonResponse<DormStuVo> getDormStuLive1(@RequestBody String  json) throws ParseException {
        JSONObject jsonObject = JSON.parseObject(json);
        PageDTO pageDTO=jsonObject.toJavaObject(PageDTO.class);
        String a=jsonObject.getString("num");
        int num;
        if(a==null|| a.isEmpty()){
            num=-1;
        }
        else {
            num=Integer.parseInt(a);
        }
        String name=jsonObject.getString("name");
        return dormStuService.findAll1(pageDTO,num,name);
    }

    @PostMapping("/dormstu/absence/add")
    public CommonResponse<DormStu> addDormStuLive1(@Valid @RequestBody DormStu dormStu) {
        return dormStuService.add1(dormStu);
    }

    @PostMapping("/dormstu/absence/update")
    public CommonResponse<DormStu> updateDormStuLive1(@Valid @RequestBody DormStu dormStu) {
        return dormStuService.update1(dormStu);
    }

    @PostMapping("/dormstu/absence/delete")
    public CommonResponse<DormStu> deleteDormStuLive1(@Valid @RequestBody DormStu dormStu) {
        return dormStuService.delete1(dormStu);
    }
}
